#include "ValidateCardDlg.h"
#include "resource.h"
#include "AuthUserDlg.h"
#include "CardBussiness.h"
#include "common.h"

LRESULT CALLBACK ValidateCardProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{	
    CValidateCardDlg *Obj;
    if (message==WM_INITDIALOG)
    {	
        Obj=(CValidateCardDlg*)lParam;
        SetWindowLong(hWnd,GWL_USERDATA,(LONG)Obj);
    }
    Obj=(CValidateCardDlg*)GetWindowLong(hWnd,GWL_USERDATA);
    if (Obj)
        return Obj->DialogProc(hWnd, message, wParam, lParam);
    return 0;
}


CValidateCardDlg::CValidateCardDlg(HINSTANCE hInstance, HWND hParent)
{
    DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_DIALOG_VALIDATE_CARD), hParent, (DLGPROC)ValidateCardProc, (LPARAM)this);
}

CValidateCardDlg::~CValidateCardDlg(void)
{
}

LRESULT CValidateCardDlg::DialogProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HWND hEditCardID;
    switch (message) 
    {
    case WM_INITDIALOG:
        {	
            hEditCardID = GetDlgItem(hWnd, IDC_EDIT_CARDID);
            this->m_hDlg = hWnd;	
            SetFocus(hEditCardID);
            return FALSE;
        }		
    case WM_COMMAND:
        switch (LOWORD(wParam))
        {	
        case IDOK:		
            {
				CCardBussiness objCardBuss;
                hEditCardID = GetDlgItem(hWnd, IDC_EDIT_CARDID);
                strcpy(g_sCardIDInputBefore, g_sCardIDInput);
				GetWindowText(hEditCardID, g_sCardIDInput, 18);
                if (strcmp(g_sCardIDInputBefore, g_sCardIDInput) != 0)
                    g_nCount = MAX_INVALID;
				if (objCardBuss.CheckValidateCard(g_sCardIDInput, hWnd))
				{
                    EndDialog(hWnd, 0); 
					AuthUserDlg authUserDlg(hGlobalInst, hWnd);
				}
				else
                {
                    SetWindowText(hEditCardID, "");
                    SetFocus(hEditCardID);
                }
            }
            break;
        case IDCANCEL:
            EndDialog(m_hDlg, wParam);
            return TRUE;
        }
        break;
    default:
        return FALSE;
    }
    return TRUE;
}
